package com.bff.gaia.mix.api.xjoin.client.rdb.parser;/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.bff.gaia.mix.api.xjoin.core.utils.ClassUtil;
import com.bff.gaia.mix.api.xjoin.core.utils.TypeInformationUtil;
import com.bff.gaia.mix.api.xjoin.client.rdb.table.RdbSideTableInfo;

/**
 * Project Name: gaia-parent
 * Description:
 * Data: 2019/7/20 10:53
 * Company: NEU
 *
 * @author tiger
 * @version v1.0
 */
public class RdbSideTableInfoBuilders {

	private static final String MYSQL_TYPE = "mysql";

	public static MysqlTableInfoBuilder buildMysqlTableInfo(){
		return new MysqlTableInfoBuilder();
	}

	public static class MysqlTableInfoBuilder {
		private final RdbSideTableInfo mysqlSideInfo;

		public MysqlTableInfoBuilder() {
			this.mysqlSideInfo = new RdbSideTableInfo();
			mysqlSideInfo.setType(MYSQL_TYPE);
		}

		public MysqlTableInfoBuilder setUrl(String url) {
			mysqlSideInfo.setUrl(url);
			return this;
		}

		public MysqlTableInfoBuilder setTableName(String tableName) {
			mysqlSideInfo.setTableName(tableName);
			mysqlSideInfo.setName(tableName);
			return this;
		}

		public MysqlTableInfoBuilder setUserName(String userName) {
			mysqlSideInfo.setUserName(userName);
			return this;
		}

		public MysqlTableInfoBuilder setPassword(String password) {
			mysqlSideInfo.setPassword(password);
			return this;
		}

//		public RdbSideTableInfo setCacheType(ECacheType type){
//			mysqlSideInfo.setCacheType(type.name());
//			return mysqlSideInfo;
//		}
//
//		public RdbSideTableInfo setCacheSize(int cacheSize){
//			mysqlSideInfo.setCacheSize(cacheSize);
//			return mysqlSideInfo;
//		}
//
//		public RdbSideTableInfo setCacheTimeout(long cacheTimeout){
//			mysqlSideInfo.setCacheTimeout(cacheTimeout);
//			return mysqlSideInfo;
//		}

		public MysqlTableInfoBuilder addfieldInfo(String fieldName, String fieldType) {
			//TODO make sure do not exist type error
			mysqlSideInfo.addField(fieldName);
			mysqlSideInfo.addFieldType(fieldType);
			mysqlSideInfo.addFieldClass(ClassUtil.stringConvertClass(fieldType));
			mysqlSideInfo.addFieldTypeInfomation(TypeInformationUtil.stringConvertTypeInformation(fieldType));
			return this;
		}

		public MysqlTableInfoBuilder addfieldInfos(String[] fields) {
			//TODO make sure do not exist type error
			for (String str : fields) {
				String fieldName = str.split(" ")[0];
				String fieldType = str.split(" ")[1];
				mysqlSideInfo.addField(fieldName);
				mysqlSideInfo.addFieldType(fieldType);
				mysqlSideInfo.addFieldClass(ClassUtil.stringConvertClass(fieldType));
				mysqlSideInfo.addFieldTypeInfomation(TypeInformationUtil.stringConvertTypeInformation(fieldType));
			}
			return this;
		}

		public RdbSideTableInfo finish() {
			mysqlSideInfo.finish();
			return mysqlSideInfo;
		}
	}
}